package 笔试题;

public class lk_765 {
    public static void main(String[] args) {
        lk_765 m = new lk_765();

        System.out.println(m.minSwapsCouples(new int[]{9,12,2,10,11,0,13,6,4,5,3,8,1,7}));
    }
    public int minSwapsCouples(int[] row) {
        int[] re = new int[row.length>>1];
        for (int i = 0; i < row.length; i++) {
            row[i] = row[i]>>1;
        }
        for (int i = 0; i < row.length>>1; i++) {
            row[i] = row[i]>>1;
            re[i] = -1;
        }
        for (int i = 0; i < row.length; i+=2) {
            if(re[row[i]>>1] >=0){
                re[row[i+1]>>1] = row[i]>>1;
            }else{
                re[row[i]>>1] = row[i+1]>>1;
            }
        }
        int k = 0;
        int sum = 0;
        int zz = 0;
        for (int i = 0; i < re.length; i++) {
            int z = i;
            if(re[z]>=0&&re[i]!=i){
                k++;
                while (re[z]>=0){
                    sum ++;
                    zz = re[z];
                    re[z] = -1;
                    z = zz;
                    if(z <0){
                        break;
                    }
                }
            }

        }
        return sum-k;
    }

}
